import java.awt.Color;
import java.util.ArrayList;

/**
 * Holds the gamemap which stores the positions of each snakebot
 * with an int[][].
 * The class itself is not static but every field and method in it
 * is which means that there only is one GameMap and all classes
 * can get info from it without having to get an reference to the object
 * used as the gamemap.
 * @author Patrik Berggren
 *
 */
public class GameMap {
	public static enum Direction {
	    RIGHT,UP,LEFT,DOWN 
	}
	
	private static int[][] map;
	private static int players;
	private static int side;
	private static ArrayList<Color> playerColors;
	
	/**
	 * Creates an GameMap
	 * @param sideLength is specified to be the length of the square
	 * gamemap. All sideelements gets the value 100 to create an edge.
	 * @param numOfPlayers the number of players that will be on the map
	 */
	public GameMap(int sideLength, int numOfPlayers){
		side=sideLength+2;
		map=new int[side][side];
		players=numOfPlayers;
		generateColors();
		clearMap();
	}
	
	/**
	 * All sideelements is given the value 100 to create an edge
	 * around the gamemap. All other elements is cleared by
	 * asigning them the value zero.
	 */
	public static void clearMap(){
		for(int x=0;x<side;x++){
			for(int y=0;y<side;y++){
				map[x][y]=0;
			}
		}
		for(int i=0;i<side;i++){
			map[i][0]=100;
			map[0][i]=100;
			map[i][side-1]=100;
			map[side-1][i]=100;
		}
	}
	
	/**
	 * Generates colors for all players
	 */
	private void generateColors(){
		playerColors=new ArrayList<Color>();
		int i;
		for(i=1;i*(i*i-1)<players;i++){
		}
		
		for(int r=0;r<255;r+=255/i){
			for(int g=0;g<255;g+=255/i){
				for(int b=0;b<255;b+=255/i){
					if(!(r==g&&r==b&&g==b))
					playerColors.add(new Color(r, g, b));
				}
			}
		}
	}
	
	/**
	 * Returns the position of the player with Id specified by
	 * the parameter playerId.
	 * @param playerId
	 * @return (x,y) position with an int[].
	 */
	public static int [] getPlayerPosition(int playerId){
		for(int x=0;x<side;x++){
			for(int y=0;y<side;y++){
				if(map[x][y]==-playerId){
					return new int[]{x,y};
				}
			}
		}
		return null;
	}
	
	/**
	 * Return value of the position specified by
	 * the (x,y) position specified by the parameters
	 * @param x
	 * @param y
	 * @return value of the (x,y) position of the gamemap
	 */
	public static int getPosition(int x, int y){
		if(x<0||x>=side||y<0||y>=side)
			return 100;
		return map[x][y];
	}
	
	/**
	 * 
	 * @return sideLength of the square gamemap
	 */
	public static int getSideLength(){
		return side;
	}
	
	/**
	 * sets the specified (x,y) position to the value specified
	 * by the parameter value.
	 * @param x
	 * @param y
	 * @param value
	 */
	public static void setPosition(int x, int y, int value){
		map[x][y]=value;
	}
	
	/**
	 * @param playerId
	 * @return Color of the player with ID playerId
	 */
	public static Color getColor(int playerId){
		if(playerId<0)
			playerId=-playerId;
		return playerColors.get(playerId-1);
	}

}
